class ResourceManager:
    def __init__(self):
        # 支持多种资源类型
        self.resource_ids = {
            "bag": 0x0E76,  # 袋子
            "ore": 0x19B9,  # 矿石
            "log": 0x1BDD,  # 木材
        }
        self.alarm_thresholds = {
            "bag": 2,
            "ore": 10,
            "log": 10,
        }

    def find_ground_resources(self, resource_name):
        if resource_name not in self.resource_ids:
            print(f"Resource '{resource_name}' not configured.")
            return []
        resource_id = self.resource_ids[resource_name]
        filter = Items.Filter()
        filter.Enabled = True
        filter.OnGround = True
        filter.Graphics.Add(resource_id)
        items = Items.ApplyFilter(filter)
        print(f"Found {len(items)} '{resource_name}' on ground.")
        return items

    def find_backpack_resources(self, resource_name):
        if resource_name not in self.resource_ids:
            print(f"Resource '{resource_name}' not configured.")
            return []
        resource_id = self.resource_ids[resource_name]
        filter = Items.Filter()
        filter.Enabled = True
        filter.OnGround = False
        filter.Container = Player.Backpack.Serial
        filter.Graphics.Add(resource_id)
        items = Items.ApplyFilter(filter)
        print(f"Found {len(items)} '{resource_name}' in backpack.")
        return items

    def count_resources(self, resource_name, location="ground"):
        if location == "ground":
            items = self.find_ground_resources(resource_name)
        elif location == "backpack":
            items = self.find_backpack_resources(resource_name)
        else:
            print(f"Unknown location: {location}")
            return 0
        return len(items)

    def group_resources_by_z(self, resource_name):
        items = self.find_ground_resources(resource_name)
        grouped = {}
        for item in items:
            z = item.Position.Z
            grouped.setdefault(z, []).append(item)
        return grouped

    def print_resource_locations(self, resource_name, location="ground"):
        if location == "ground":
            items = self.find_ground_resources(resource_name)
        elif location == "backpack":
            items = self.find_backpack_resources(resource_name)
        else:
            print(f"Unknown location: {location}")
            return
        for item in items:
            x, y, z = item.Position.X, item.Position.Y, item.Position.Z
            msg = f"Found {resource_name} at ({x}, {y}, {z})"
            print(msg)
            Player.ChatSay(msg)

    def move_ground_resource_to_backpack(self, resource_name, max_count=1):
        items = self.find_ground_resources(resource_name)
        moved = 0
        for item in items:
            if moved >= max_count:
                break
            Items.Move(item, Player.Backpack.Serial, 0)
            Misc.Pause(600)
            moved += 1
            print(f"Moved {resource_name} (serial: {item.Serial}) to backpack.")
        if moved == 0:
            print(f"No {resource_name} found to move.")
        else:
            print(f"Moved {moved} {resource_name}(s) to backpack.")

    def alarm_if_low(self, resource_name, location="backpack"):
        threshold = self.alarm_thresholds.get(resource_name, 1)
        count = self.count_resources(resource_name, location)
        if count < threshold:
            msg = f"ALERT: {resource_name} is low! Only {count} left in {location}."
            print(msg)
            Player.ChatSay(msg)
        else:
            print(f"{resource_name} is sufficient: {count} in {location}.") 